﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DateReminderApp
{
    public partial class EditReminderForm : Form
    {
        public Reminder Reminder { get; private set; }

        // 控件声明
        private TextBox txtTitle;
        private TextBox txtDescription;
        private DateTimePicker dtpDate;
        private DateTimePicker dtpTime;
        private Button btnOk;
        private Button btnCancel;
        private Label lblTitle;
        private Label lblDescription;
        private Label lblDate;
        private Label lblTime;

        public EditReminderForm()
        {
            Reminder = new Reminder();
            InitializeComponent();
            SetDefaultValues();
        }

        public EditReminderForm(Reminder existingReminder)
        {
            Reminder = existingReminder ?? new Reminder();
            InitializeComponent();
            LoadReminderData();
        }

        private void InitializeComponent()
        {
            // 初始化所有控件
            txtTitle = new TextBox();
            txtDescription = new TextBox();
            dtpDate = new DateTimePicker();
            dtpTime = new DateTimePicker();
            btnOk = new Button();
            btnCancel = new Button();
            lblTitle = new Label();
            lblDescription = new Label();
            lblDate = new Label();
            lblTime = new Label();

            // 配置窗体
            this.Text = "编辑提醒";
            this.Size = new Size(450, 280);
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Padding = new Padding(10);

            // 标题标签
            lblTitle.Text = "标题:";
            lblTitle.Location = new Point(20, 20);
            lblTitle.Size = new Size(60, 20);

            // 标题文本框
            txtTitle.Location = new Point(90, 20);
            txtTitle.Size = new Size(320, 20);

            // 描述标签
            lblDescription.Text = "描述:";
            lblDescription.Location = new Point(20, 60);
            lblDescription.Size = new Size(60, 20);

            // 描述文本框
            txtDescription.Location = new Point(90, 60);
            txtDescription.Size = new Size(320, 60);
            txtDescription.Multiline = true;

            // 日期标签
            lblDate.Text = "日期:";
            lblDate.Location = new Point(20, 130);
            lblDate.Size = new Size(60, 20);

            // 日期选择器
            dtpDate.Location = new Point(90, 130);
            dtpDate.Size = new Size(120, 20);
            dtpDate.Format = DateTimePickerFormat.Custom;
            dtpDate.CustomFormat = "yyyy-MM-dd";

            // 时间标签
            lblTime.Text = "时间:";
            lblTime.Location = new Point(220, 130);
            lblTime.Size = new Size(60, 20);

            // 时间选择器
            dtpTime.Location = new Point(260, 130);
            dtpTime.Size = new Size(110, 20);
            dtpTime.Format = DateTimePickerFormat.Time;
            dtpTime.ShowUpDown = true;

            // 确定按钮
            btnOk.Text = "确定";
            btnOk.DialogResult = DialogResult.OK;
            btnOk.Location = new Point(250, 180);
            btnOk.Size = new Size(80, 30);
            btnOk.Click += BtnOk_Click;

            // 取消按钮
            btnCancel.Text = "取消";
            btnCancel.DialogResult = DialogResult.Cancel;
            btnCancel.Location = new Point(340, 180);
            btnCancel.Size = new Size(80, 30);

            // 添加控件到窗体
            this.Controls.Add(lblTitle);
            this.Controls.Add(txtTitle);
            this.Controls.Add(lblDescription);
            this.Controls.Add(txtDescription);
            this.Controls.Add(lblDate);
            this.Controls.Add(dtpDate);
            this.Controls.Add(lblTime);
            this.Controls.Add(dtpTime);
            this.Controls.Add(btnOk);
            this.Controls.Add(btnCancel);

            this.AcceptButton = btnOk;
            this.CancelButton = btnCancel;
        }

        private void BtnOk_Click(object sender, EventArgs e)
        {
            // 验证输入
            if (string.IsNullOrWhiteSpace(txtTitle.Text))
            {
                MessageBox.Show("请输入提醒标题");
                return;
            }

            // 更新Reminder对象
            Reminder.Title = txtTitle.Text.Trim();
            Reminder.Description = txtDescription.Text.Trim();

            // 合并日期和时间
            DateTime selectedDate = dtpDate.Value.Date;
            DateTime selectedTime = dtpTime.Value;
            Reminder.ReminderTime = new DateTime(
                selectedDate.Year, selectedDate.Month, selectedDate.Day,
                selectedTime.Hour, selectedTime.Minute, 0
            );

            this.DialogResult = DialogResult.OK;
        }

        private void SetDefaultValues()
        {
            // 设置默认值
            if (string.IsNullOrEmpty(Reminder.Title))
            {
                Reminder.Title = "新提醒";
            }
            if (Reminder.ReminderTime < DateTime.Now)
            {
                Reminder.ReminderTime = DateTime.Now.AddHours(1);
            }
        }

        private void LoadReminderData()
        {
            // 加载数据到控件
            txtTitle.Text = Reminder.Title;
            txtDescription.Text = Reminder.Description;
            dtpDate.Value = Reminder.ReminderTime;
            dtpTime.Value = Reminder.ReminderTime;
        }
    }
}
